//
//  PayInfoView.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/5/31.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import UIKit
import SwiftyJSON
class PayInfoView: UIView {
    var orderID = ""
    var amount = ""
    var currentTag = Int()
    var btn:UIButton!
    //支付方式数组
    lazy var payWayArray : JSON = {
//        let jsonStr =  "[{\"title\": \"微信支付\", \"picture\": \"weixin\"},{\"title\": \"支付宝\", \"picture\": \"zhifubao\"},{\"title\": \"银联支付\", \"picture\": \"yinlian\"}]"
        let jsonStr = "[{\"title\": \"微信支付\", \"picture\": \"weixin\"}]"
        let jsonData = jsonStr.data(using: String.Encoding.utf8, allowLossyConversion: false)
        return JSON(data: jsonData!)
    }()
    override func layoutSubviews() {
        super.layoutSubviews()
        let pc = PayController()
        let label = UILabel()
        label.textAlignment = .left
        label.font = UIFont.systemFont(ofSize: 13)
        label.text = "订单详情"
        label.textColor = UIColor.gray
        self.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.width.equalTo(self)
            make.height.equalTo(35)
            make.top.equalTo(self).offset(5)
            make.left.equalTo(5)
        }
        let view1 = UIView()
        view1.backgroundColor = UIColor.white
        self.addSubview(view1)
        view1.snp.makeConstraints { (make) in
            make.width.equalTo(self)
            make.height.equalTo(81)
            make.top.equalTo(label.snp.bottom)
        }
        let orderLabel =  UILabel()
        orderLabel.text = "订单号：\(orderID)"
        orderLabel.font = UIFont.systemFont(ofSize: 13)
        orderLabel.textColor = UIColor.gray
        view1.addSubview(orderLabel)
        orderLabel.snp.makeConstraints { (make) in
            make.width.equalTo(view1.snp.width)
            make.height.equalTo(35)
            make.top.equalTo(view1.snp.top).offset(5)
            make.left.equalTo(5)
        }
        //细分割线
        let bseparator1 = UILabel()
        bseparator1.backgroundColor = tableBgColor
        view1.addSubview(bseparator1)
        bseparator1.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.top.equalTo(orderLabel.snp.bottom)
            make.left.equalTo(self).offset(5)
            make.right.equalTo(self).offset(-5)
        }
        
        let amountLabel =  UILabel()
        amountLabel.text = "订单金额："
        amountLabel.font = UIFont.systemFont(ofSize: 13)
        amountLabel.textColor = UIColor.gray
        view1.addSubview(amountLabel)
        amountLabel.snp.makeConstraints { (make) in
            make.width.equalTo(70)
            make.height.equalTo(35)
            make.top.equalTo(bseparator1.snp.bottom).offset(1)
            make.left.equalTo(5)
        }
        let money =  UILabel()
        money.text = "￥\(amount)"
        money.font = UIFont.systemFont(ofSize: 13)
        money.textColor = UIColor.red
        view1.addSubview(money)
        money.snp.makeConstraints { (make) in
            make.width.equalTo(view1.snp.width).offset(-60)
            make.height.equalTo(35)
            make.top.equalTo(bseparator1.snp.bottom).offset(1)
            make.left.equalTo(75)
        }
        let label2 = UILabel()
        label2.textAlignment = .left
        label2.font = UIFont.systemFont(ofSize: 13)
        label2.text = "请选择支付方式"
        label2.textColor = UIColor.gray
        self.addSubview(label2)
        label2.snp.makeConstraints { (make) in
            make.width.equalTo(self)
            make.height.equalTo(35)
            make.top.equalTo(money.snp.bottom).offset(10)
            make.left.equalTo(5)
        }
        //支付方式
        let view2 = UIView()
        view2.backgroundColor = UIColor.white
        self.addSubview(view2)
        view2.snp.makeConstraints { (make) in
            make.width.equalTo(self)
            make.height.equalTo(40*payWayArray.count+2)
            make.top.equalTo(label2.snp.bottom)
        }
        for i:Int in 0 ..< payWayArray.count {
            let model = PayWayModel.init(dic: payWayArray[i])
            let leftImage = UIImageView(image: UIImage(named: "\(model.picture)"))
            view2.addSubview(leftImage)
            leftImage.snp.makeConstraints { (make) in
                make.width.height.equalTo(21)
                make.left.equalTo(10)
                make.top.equalTo(view2.snp.top).offset(40*i+9)
            }
            let payLabel = UILabel()
            payLabel.text = model.title
            payLabel.font = UIFont.systemFont(ofSize: 14)
            view2.addSubview(payLabel)
            payLabel.snp.makeConstraints { (make) in
                make.width.equalTo(60)
                make.height.equalTo(40)
                make.top.equalTo(view2.snp.top).offset(40*i)
                make.left.equalTo(leftImage.snp.right).offset(15)
            }
            let radioBtn = UIButton()
            radioBtn.setImage(UIImage(named: "radio_checked"), for: .selected)
            radioBtn.setImage(UIImage(named: "radio"), for: .normal)
            if i == 0 {
                radioBtn.isSelected = true
            }else{
                radioBtn.isSelected = false
            }
            radioBtn.tag = i+1000;
            radioBtn.addTarget(self, action: #selector(radioBtnClick(sender:)), for: UIControlEvents.touchUpInside)
            view2.addSubview(radioBtn)
            radioBtn.snp.makeConstraints { (make) in
                make.right.equalTo(view2.snp.right).offset(-20)
                make.width.height.equalTo(20)
                make.top.equalTo(view2.snp.top).offset(40*i+10)
            }
            if i !=  payWayArray.count - 1 {
                //细分割线
                let bseparator2 = UILabel()
                bseparator2.backgroundColor = tableBgColor
                view2.addSubview(bseparator2)
                bseparator2.snp.makeConstraints { (make) in
                    make.height.equalTo(1)
                    make.top.equalTo(payLabel.snp.bottom)
                    make.left.equalTo(self).offset(5)
                    make.right.equalTo(self).offset(-5)
                }
            }
            currentTag = 1000;
        }
        //底部按钮
        let bottomView = UIView()
        bottomView.backgroundColor = UIColor.white
        self.addSubview(bottomView)
        bottomView.snp.makeConstraints { (make) in
            make.height.equalTo(127)
            make.width.equalTo(self)
            make.bottom.equalTo(self.snp.bottom).offset(-87)
        }
        let noticeLabel = UILabel()
        noticeLabel.textAlignment = .center
        noticeLabel.font = UIFont.systemFont(ofSize: 12)
        noticeLabel.text = "由于各个银行的单日、单笔交易会有最高限额。 如果订单超出支付限额，请分批提交缴费订单哦~"
        noticeLabel.numberOfLines = 2
        noticeLabel.textColor = UIColor.gray
        bottomView.addSubview(noticeLabel)
        noticeLabel.snp.makeConstraints { (make) in
            make.width.equalTo(265)
            make.height.equalTo(33)
            make.top.equalTo(bottomView.snp.top).offset(5)
            make.centerX.equalTo(bottomView.snp.centerX)
        }
        //细分割线
        let bseparator3 = UILabel()
        bseparator3.backgroundColor = tableBgColor
        bottomView.addSubview(bseparator3)
        bseparator3.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.top.equalTo(noticeLabel.snp.bottom).offset(1)
            make.left.equalTo(self)
            make.right.equalTo(self)
        }
        //支付协议
        let view3 = UIView()
        view3.backgroundColor = UIColor.white
        self.addSubview(view3)
        view3.snp.makeConstraints { (make) in
            make.width.equalTo(self)
            make.height.equalTo(37)
            make.top.equalTo(bseparator3.snp.bottom)
        }
        let checkBtn = UIButton()
        checkBtn.setImage(UIImage(named: "checked"), for: .selected)
        checkBtn.setImage(UIImage(named: "check"), for: .normal)
        checkBtn.addTarget(pc, action: #selector(pc.checkClick(sender:)), for: UIControlEvents.touchUpInside)
        view3.addSubview(checkBtn)
        checkBtn.snp.makeConstraints { (make) in
            make.height.width.equalTo(25)
            make.centerY.equalTo(view3.snp.centerY)
            make.left.equalTo(view3).offset(10)
        }
        let mianzeLabel =  UILabel()
        mianzeLabel.text = "支付协议免责声明"
        mianzeLabel.font = UIFont.systemFont(ofSize: 12)
        view3.addSubview(mianzeLabel)
        mianzeLabel.snp.makeConstraints { (make) in
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.top.equalTo(view3.snp.top).offset(2)
            make.left.equalTo(checkBtn.snp.right)
        }
        let rightBtn = UIButton()
        rightBtn.setBackgroundImage(UIImage(named:"icon_right"), for: .normal)
        rightBtn.addTarget(pc, action: #selector(pc.openHtml(sender:)), for: UIControlEvents.touchUpInside)
        view3.addSubview(rightBtn)
        rightBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(20)
            make.centerY.equalTo(view3.snp.centerY)
            make.right.equalTo(view3.snp.right).offset(-10)
        }
        //细分割线
        let bseparator5 = UILabel()
        bseparator5.backgroundColor = tableBgColor
        bottomView.addSubview(bseparator5)
        bseparator5.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.top.equalTo(view3.snp.bottom).offset(1)
            make.left.equalTo(self)
            make.right.equalTo(self)
        }
        btn = UIButton()
        btn.setTitle("立即支付", for: .normal)
        btn.backgroundColor = appMainColor
        btn.tintColor = UIColor.white
        btn.layer.cornerRadius = 5
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.addTarget(pc, action: #selector(pc.payClick(sender:)), for: UIControlEvents.touchUpInside)
        bottomView.addSubview(btn)
        btn.snp.makeConstraints { (make) in
            make.height.equalTo(35)
            make.left.equalTo(30)
            make.right.equalTo(-30)
            make.bottom.equalTo(bottomView.snp.bottom).offset(-5)
        }
    }
    func radioBtnClick(sender:UIButton){
        if !sender.isSelected {
            sender.isSelected = !sender.isSelected;
            //上一个按钮还原
            let buttonN = self.viewWithTag(currentTag) as? UIButton;
            buttonN?.isSelected = false;
            currentTag = sender.tag;
        }
    }
}
